package com.simple.pixelcopydemo

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.media.MediaPlayer
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.PixelCopy
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.Button
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.graphics.createBitmap

class SurfaceViewActivity : AppCompatActivity() {
    private lateinit var surfaceView: SurfaceView
    private lateinit var mediaPlayer: MediaPlayer

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_surface_view)

        surfaceView = findViewById(R.id.surface_view)
        val button = findViewById<Button>(R.id.btn_capture)

        surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                mediaPlayer = MediaPlayer().apply {
                    setDataSource("https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")
                    setDisplay(holder)
                    isLooping = true
                    prepareAsync()
                    setOnPreparedListener { start() }
                }
            }

            override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
            override fun surfaceDestroyed(holder: SurfaceHolder) {}
        })

        button.setOnClickListener {
            val bitmap = createBitmap(window.decorView.width, window.decorView.height, Bitmap.Config.RGB_565)
            PixelCopy.request(window, bitmap, { result ->
                if (result == PixelCopy.SUCCESS) {
                    showBitmap(bitmap)
                }
            }, Handler(Looper.getMainLooper()))
        }

        findViewById<Button>(R.id.btn_capture_surface).setOnClickListener {
            val bitmap = createBitmap(surfaceView.width, surfaceView.height)
            /**
             * 可以获取SurfaceView加载的视频截图
             * surfaceView:截图的窗口,不能为空
             * rect:截图区域，相对于 surfaceView 的位置坐标,可以为空,为空就是整个window的大小
             * bitmap:用来接收截图的 Bitmap,不能为空
             * handler:处理截图结果回调的线程，一般用 MainLooper
             */
            PixelCopy.request(surfaceView, bitmap, { result ->
                if (result == PixelCopy.SUCCESS) {
                    showBitmap(bitmap)
                }
            }, Handler(Looper.getMainLooper()))

        }
    }

    private fun showBitmap(bitmap: Bitmap) {
        val imageView = ImageView(this).apply { setImageBitmap(bitmap) }
        AlertDialog.Builder(this).setTitle("截图").setView(imageView).setPositiveButton("关闭", null).show()
    }
}
